package com.xone.xapp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.xone.xapp.HttpUtils.OnPostSuccessListener;

public class RegisterActivity extends BasicActivity {
	protected final String TAG = RegisterActivity.class.getSimpleName();
	protected Map<String, String> infoMap = new LinkedHashMap<String, String>();
	
	@Override
	public String getHeaderTitle() {
		return "用户注册";
	}

	@Override
	public int getContentView() {
		return R.layout.register;
	}
	/**
	 * 是否需要检测登录
	 * @return
	 */
	@Override
	public boolean isLoginCheck() {
		return false;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		infoMap.put("username", "用户名称不能为空");
		infoMap.put("cellphone", "手机不能为空");
		infoMap.put("contactor", "联系人不能为空");
		infoMap.put("nickName", "");
		infoMap.put("qq", "");
		infoMap.put("email", "");
		infoMap.put("address", "");
		infoMap.put("password", "用户密码不能为空");
		infoMap.put("repassword", "确认密码不能为空");
		Button submitButton = (Button)findViewById(R.id.submit_user_register);
		submitButton.setOnClickListener(new OnClickListener() {
			@SuppressWarnings("unchecked")
			@Override
			public void onClick(View v) {
				Map<String, String> value = checkAndGetValue();
				if (value.isEmpty()) {
					return;
				}
				if (!value.get("password").equals(value.get("repassword"))) {
					Toast.makeText(getContext(), "密码与确认密码必需一致", Toast.LENGTH_SHORT).show();
					return;
				}
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				for (Map.Entry<String, String> info : value.entrySet()) {
					params.add(new BasicNameValuePair("person." + info.getKey(), info.getValue()));
				}
				new RegisterDataTask(getContext()).execute(params);
			}
		});
	}
	
	protected boolean notPass(String value, String notice) {
		if (StringUtils.isBlank(value)) {
			Toast.makeText(getContext(), notice, Toast.LENGTH_SHORT).show();
			return true;
		}
		return false;
	}
	
	public Map<String, String> checkAndGetValue() {
		Map<String, String> valueMap = new HashMap<String, String>();
		for (Map.Entry<String, String> info : infoMap.entrySet()) {
			EditText editText = (EditText)findViewById(ContextUtils.getResourceId(getContext(), info.getKey(), "id"));
			String value = editText.getText().toString();
			if (StringUtils.isNotBlank(info.getValue()) && notPass(value, info.getValue())) {
				return Collections.emptyMap();
			}
			valueMap.put(info.getKey(), value);
		}
		return valueMap;
	}
	
	protected class RegisterDataTask extends AsyncTask<List<NameValuePair>, Integer, JSONObject> {
		protected Context context;
		public RegisterDataTask(Context context) {
			this.context = context;
		}

		@Override
		protected JSONObject doInBackground(List<NameValuePair>... params) {
			if (isCancelled() || null == params || params.length == 0) {
				return null;
			}
			return HttpUtils.httpPost(Constants.APP_URL + "/login/registerJson.html", params[0], new OnPostSuccessListener<JSONObject>() {
				@Override
				public JSONObject onSuccess(int status, String result) {
					return JSONUtils.getJSONObjectFromString(result);
				}
			}, getContext());
		}
		
		@Override
		protected void onPostExecute(JSONObject object) {
			super.onPostExecute(object);
			hideProgress();
			if (null == object) {
				Toast.makeText(getContext(), "操作失败", Toast.LENGTH_SHORT).show();
				return;
			}
			JSONObject mapValue = JSONUtils.getJSONObjectForKey(object, "mapValue");
			Toast.makeText(getContext(), JSONUtils.getStringFromJSONObject(mapValue, "msg"), Toast.LENGTH_SHORT).show();
			if (!mapValue.isNull("user")) {//注册成功
				getContext().startActivity(new Intent(getContext(), LoginActivity.class));
			}
		}
	}

}
